/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.netconsole.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.struts2.util.ServletContextAware;
import org.hibernate.SessionFactory;
import org.netconsole.dao.EquipmentDAO;
import org.netconsole.dao.EquipmentDAOImpl;
import org.netconsole.model.Equipment;

/**
 *
 * @author aurel
 */
public class EquipmentAction extends ActionSupport implements Action, ModelDriven<Equipment>, ServletContextAware{

    private Equipment equipment = new Equipment();
    private List<Equipment> lstEq;
    private ServletContext ctx;

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }

    @Override
    public Equipment getModel() {
        return equipment;
    }

    @Override
    public void setServletContext(ServletContext ctx) {
        this.ctx = ctx;
    }
    
    public List<Equipment> getLstEq() {
        return lstEq;
    }

    public void setLst(List<Equipment> lstEq) {
        this.lstEq = lstEq;
    }
    
    public String listEquipment() throws Exception {
        SessionFactory sf =(SessionFactory) ctx.getAttribute("SessionFactory");
        EquipmentDAO eqDAO = new EquipmentDAOImpl(sf);
        lstEq = eqDAO.listEquipment();

        System.out.println(lstEq.size() + " equipmentAction !! \n\n");
        for (int i=0; i<lstEq.size(); i++){
            System.out.println(lstEq.get(i).toString());
        }
        return SUCCESS;
    }
    
    public String insertEq() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        EquipmentDAO eqDAO = new EquipmentDAOImpl(sf);
	Equipment eqDB = eqDAO.insertEq(equipment);
        listEquipment();
        return SUCCESS;
    }
    
    public String modifForm() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        EquipmentDAO eqDAO = new EquipmentDAOImpl(sf);
	Equipment eqDB = eqDAO.getEquipmentByID(Integer.toString(equipment.getId()));
        if (eqDB != null){
            return SUCCESS;
        }
        else{
            return ERROR;
        }
    }  
    
    public String modifEq() throws Exception {
        SessionFactory sf = (SessionFactory) ctx.getAttribute("SessionFactory");
        EquipmentDAO eqDAO = new EquipmentDAOImpl(sf);
	Equipment eqDB = eqDAO.modifEq(equipment);
        listEquipment();
        return SUCCESS;
    }
}
